﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace WebFetch
{
    public static class DatabaseHelper
    {
        public static DataTable ExecuteStoredProcedure(string procedureName, params object[] parameterValues)
        {
            DataTable dt = null;
            try
            {
                //string connectToSQL = "Data Source=localhost;Initial Catalog=Firebird;Integrated Security=True";
                string connectToSQL = ConfigurationManager.ConnectionStrings["DefaultConnectionString"].ConnectionString;

                using (SqlConnection con = new SqlConnection(connectToSQL))
                {
                    con.Open();
                    using (SqlCommand command = new SqlCommand("", con))
                    {
                        command.CommandType = CommandType.StoredProcedure;
                        command.CommandText = procedureName;
                        //command.Parameters.Add("@int", SqlDbType.Int);
                        for (int i = 0; i < parameterValues.Length; i++)
                            command.Parameters[i].Value = parameterValues[i];
                        using (SqlDataAdapter reader = new SqlDataAdapter(command))
                        {
                            dt = new DataTable();
                            reader.Fill(dt);
                        }
                    }
                }
                return dt;
            }
            catch (InvalidOperationException ioe)
            {
                return null;
            }
            catch (Exception e)
            {
                return null;
            }
            return dt;
        }
    }
}
